package mortal.game.control
{
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.debug.*;
    import com.mui.controls.*;
    import flash.events.*;
    import modules.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.model.dialog.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.rules.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.layer.utils.*;
    import mortal.game.scene.map.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class DialogController extends Controller
    {
        private var _operateInfo:OperateInfo;

        public function DialogController()
        {
            
        }

        private function onInNpcDestance(event:DataEvent) : void
        {
            var _loc_2:* = event.data as NPCInfo;
            cache.dialog.npcInfo = _loc_2;
            cache.dialog.updateNpcOperate(_loc_2.tnpc.effectDes);
            this.taskListReq();
            RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.checkOutDistance);
            Dispatcher.addEventListener(EventName.Convey_Res, this.checkOutDistance);
            Dispatcher.addEventListener(EventName.ChangeScene, this.checkOutDistance);
            cache.guide.npcBuyTask = cache.task.getBuyTask(_loc_2.tnpc.npcId);
            cache.guide.npcRepairTask = cache.task.getRepairTask(_loc_2.tnpc.npcId);
            
        }

        private function taskListReq() : void
        {
            if (cache.guide.guideSkillLearn)
            {
                GameManager.instance.popupWindow(ModuleType.Skills);
                
            }
            if (cache.guide.petGuide && !cache.guide.petBattle)
            {
                GameManager.instance.popupWindow(ModuleType.Pets);
                
            }
            if (cache.guide.isGuideNpcEffect(cache.dialog.npcInfo.tnpc.npcId))
            {
                this._operateInfo = cache.dialog.operateList[0];
                if (this._operateInfo != null)
                {
                    GameManager.instance.popupWindow(this._operateInfo.operateId);
                    this._operateInfo = null;
                    
                }
            }
            if (cache.guide.guideUseDrug)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_UseDrug));
                
            }
            if (cache.guide.guideEquipRefresh && cache.guide.isGuideEquipRefresh(cache.dialog.npcInfo.tnpc.npcId))
            {
                GameManager.instance.popupWindow(ModuleType.Build);
                
            }
            if (cache.guide.guidePetRefresh && cache.guide.isGuidePetRefresh(cache.dialog.npcInfo.tnpc.npcId))
            {
                GameController.pet.view.show();
                
            }
            if (cache.scene.isGetRunBussinessNpc(cache.dialog.npcInfo.tnpc.npcId) && !cache.guild.isInSelfManorMap())
            {
                this.onTaskListRes([]);
            }
            else
            {
                GameProxy.dialogProxy.npcTaskReq(cache.dialog.npcInfo.tnpc.npcId);
            }
            
        }

        private function onTaskInfoReq(event:DataEvent) : void
        {
            var _loc_3:TaskInfo = null;
            var _loc_2:* = event.data as DialogInfo;
            if (_loc_2.operateId == DialogOperate.GetTask || _loc_2.status == ETaskStatus._ETaskStatusCanGet)
            {
                this.onTaskInfoRes(_loc_2);
            }
            else
            {
                _loc_3 = cache.task.getTaskById(_loc_2.sTask.code);
                if (_loc_3 != null)
                {
                    if (_loc_3.sTask.group == ETaskGroup._ETaskGroupTransport)
                    {
                        GameProxy.transport.transportTaskInfoReq(cache.dialog.npcInfo.tnpc.npcId);
                    }
                    else
                    {
                        _loc_2 = cache.dialog.updateTaskInfo(_loc_3.sTask);
                        this.onTaskInfoRes(_loc_2);
                    }
                }
            }
            
        }

        private function onTaskDoReq(event:DataEvent) : void
        {
            var _loc_2:* = cache.dialog.dialogInfo;
            if (_loc_2.operateId == DialogOperate.DoTask)
            {
                GameProxy.taskProxy.doTaskReq(_loc_2.sTask.code, cache.dialog.npcInfo.tnpc.npcId, _loc_2.code);
            }
            
        }

        private function onTaskEndReq(event:DataEvent) : void
        {
            var _loc_2:* = cache.dialog.dialogInfo;
            if (_loc_2.operateId == DialogOperate.ListTask)
            {
                this.onTaskInfoRes(_loc_2);
            }
            else if (_loc_2.sTask.group == ETaskGroup._ETaskGroupGuild)
            {
                GameProxy.guildTask.endTaskReq(cache.dialog.npcInfo.tnpc.npcId, _loc_2.sTask.code);
            }
            else
            {
                GameProxy.taskProxy.endTaskReq(cache.dialog.npcInfo.tnpc.npcId, _loc_2.sTask.code, _loc_2.sTask.endChoose);
            }
            
        }

        private function onTaskGetReq(event:DataEvent) : void
        {
            var _loc_2:* = cache.dialog.dialogInfo;
            if (!_loc_2 || cache.dialog.taskList.length == 0 && _loc_2.operateId == DialogOperate.ListTask)
            {
                if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcGuildTask) && cache.daily.canGetGuildTask())
                {
                    GameProxy.guildTask.openGuildTask(cache.dialog.npcInfo.tnpc.npcId);
                }
                else if (NpcEffectRule.hasNpcEffect(cache.dialog.operateList, ENpcEffect._ENpcEffectHuntTask) && cache.daily.canGetHuntTask())
                {
                    GameProxy.huntProxy.openTreasureHunt(cache.dialog.npcInfo.tnpc.npcId);
                }
            }
            else if (_loc_2.operateId == DialogOperate.ListTask)
            {
                this.onTaskInfoRes(_loc_2);
            }
            else if (_loc_2.sTask.group == ETaskGroup._ETaskGroupGuild)
            {
                GameProxy.guildTask.getTaskReq(cache.dialog.npcInfo.tnpc.npcId, _loc_2.sTask.code);
            }
            else
            {
                GameProxy.taskProxy.getTaskReq(cache.dialog.npcInfo.tnpc.npcId, _loc_2.sTask.code);
            }
            
        }

        private function onNpcOptReq(event:DataEvent) : void
        {
            var _loc_2:SceneInfo = null;
            this._operateInfo = event.data as OperateInfo;
            if (this._operateInfo.operateId == ENpcEffect._ENpcEffectPass)
            {
                if (cache.task.inTransport())
                {
                    MsgManager.addBroadCast("护送中不能传送");
                    
                }
                if (Cache.instance.role.entityInfo.level >= GameConst.SetFightModeLevel)
                {
                    if (RolePlayer.instance.entityInfo.entityInfo.fightModel == EEntityFightMode._EEntityFightModePeace)
                    {
                        if (GameMapUtil.isEnemyMap(this._operateInfo.passTo.mapId))
                        {
                            _loc_2 = SceneConfig.instance.getSceneInfo(this._operateInfo.passTo.mapId);
                            Alert.show("你将前往" + _loc_2.sMapDefine.name + "，进入后将被强制开启为战斗状态。", null, Alert.OK | Alert.CANCEL, null, this.onAlertHandler);
                        }
                        else
                        {
                            this.onAlertHandler(Alert.OK);
                        }
                    }
                    else
                    {
                        this.onAlertHandler(Alert.OK);
                    }
                }
                else if (GameMapUtil.isEnemyMap(this._operateInfo.passTo.mapId))
                {
                    Alert.show("等级不够" + GameConst.SetFightModeLevel + "级，不能进入敌对阵营");
                }
                else
                {
                    this.onAlertHandler(Alert.OK);
                }
            }
            else if (this._operateInfo.operateId == ENpcEffect._ENpcEffectTransport)
            {
                GameProxy.transport.talkToTransportNPC(cache.dialog.npcInfo.tnpc.npcId);
            }
            else if (this._operateInfo.operateId == ENpcEffect._ENpcGuildTask)
            {
                GameProxy.guildTask.openGuildTask(cache.dialog.npcInfo.tnpc.npcId);
            }
            else if (this._operateInfo.operateId == GuideOptType.GuideGoToGuildTaskBoss)
            {
                GameProxy.sceneProxy.pass(EPassType.EPassTypeGuildTask, this._operateInfo.targetInfo.mapId, this._operateInfo.targetInfo.x, this._operateInfo.targetInfo.y);
                view.hide();
            }
            else if (this._operateInfo.operateId == ENpcEffect._ENpcEffectHuntTask)
            {
                GameProxy.huntProxy.openTreasureHunt(cache.dialog.npcInfo.tnpc.npcId);
            }
            else if (this._operateInfo.operateId == ENpcEffect._ENpcGuildGetRunBusiness)
            {
                if (cache.role.entityInfo.level < GameConst.GuildCreatePlayerLevel)
                {
                    MsgManager.addBroadCast("等级达到" + GameConst.GuildCreatePlayerLevel + "级才能领取跑商任务");
                    
                }
                if (cache.daily.runBussinessInfo.num >= cache.daily.runBussinessInfo.totalNum)
                {
                    if (!cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupRunBusiness))
                    {
                        MsgManager.addBroadCast("今天跑商次数已满，请明天再来");
                        
                    }
                }
                GameProxy.dialogProxy.npcTaskReq(cache.dialog.npcInfo.tnpc.npcId);
            }
            else if (this._operateInfo.operateId == GuideOptType.GuideCancelRunBuiness)
            {
                Alert.show("任务尚未完成，是否取消？", null, Alert.OK | Alert.CANCEL, null, this.onCancelTaskAlertHandler);
            }
            else if (this._operateInfo.operateId == ENpcEffect._ENpcGuildHandInItem)
            {
                GameProxy.dialogProxy.handInItemReq();
            }
            else
            {
                if (this._operateInfo.operateId == ENpcEffect._ENpcGuildFire)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildFlameAddFireWood));
                    
                }
                if (!view.isHide)
                {
                    view.hide();
                }
                GameManager.instance.popupWindow(this._operateInfo.operateId);
                this._operateInfo = null;
            }
            
        }

        private function onAlertHandler(param1:int) : void
        {
            if (param1 == Alert.OK)
            {
                if (!view.isHide)
                {
                    view.hide();
                }
                GameProxy.sceneProxy.pass(EPassType.EPassTypeNpc, cache.dialog.npcInfo.tnpc.npcId, this._operateInfo.passTo.passToId);
            }
            this._operateInfo = null;
            
        }

        private function onCancelTaskAlertHandler(param1:int) : void
        {
            if (param1 == Alert.OK)
            {
                if (!view.isHide)
                {
                    view.hide();
                }
                GameProxy.taskProxy.cancelRunBuinessTask();
            }
            this._operateInfo = null;
            
        }

        private function onTaskListRes(param1:Array) : void
        {
            var _loc_3:DialogInfo = null;
            var _loc_4:OperateInfo = null;
            var _loc_2:* = param1.length;
            if (_loc_2 == 1)
            {
                _loc_3 = param1[0];
                if (_loc_3.sTask.group == ETaskGroup._ETaskGroupTransport)
                {
                    GameProxy.transport.transportTaskInfoReq(cache.dialog.npcInfo.tnpc.npcId);
                }
                else
                {
                    GameProxy.dialogProxy.taskInfoReq(_loc_3.sTask.code);
                }
                
            }
            else if (_loc_2 == 0)
            {
                _loc_4 = cache.dialog.operateList[0];
                if (cache.dialog.operateList.length == 1 && !cache.dialog.isCopyNpc() && !NpcEffectRule.isGuildFunId(_loc_4.operateId) && !NpcEffectRule.notEffectReq(_loc_4.operateId))
                {
                    if (!view.isHide)
                    {
                        view.hide();
                    }
                    GameManager.instance.popupWindow(_loc_4.operateId);
                    
                }
                if (cache.dialog.operateList.length == 1 && _loc_4.operateId == ENpcEffect._ENpcEffectTransport)
                {
                    GameProxy.transport.talkToTransportNPC(cache.dialog.npcInfo.tnpc.npcId);
                    if (!view.isHide)
                    {
                        view.hide();
                    }
                    
                }
                if (cache.dialog.operateList.length >= 1 && _loc_4.operateId == ENpcEffect._ENpcGuildFire)
                {
                    GameProxy.guildProxy.getFireWood(cache.guild.myGuildInfo.guildId);
                }
            }
            if (view.isHide)
            {
                this.openView();
            }
            (view as IDialogModule).updateTaskList(param1, cache.dialog.operateList);
            
        }

        private function onTaskInfoRes(param1:DialogInfo) : void
        {
            if (param1.sTask != null)
            {
                cache.dialog.dialogInfo = param1;
                if (param1.sTask.guide & GuideType.UpdateCareer && !cache.guide.selectedCareer && cache.role.entityInfo.career == ECareer._ECareerNewPlayer && param1.status == ETaskStatus._ETaskStatusHadCompleted)
                {
                    if (!view.isHide)
                    {
                        view.hide();
                    }
                    GameController.career.view.show();
                    MsgManager.addBroadCast("请选择职业后再完成任务");
                    
                }
                if (view.isHide)
                {
                    this.openView();
                }
                (view as IDialogModule).updateTaskInfo(param1);
            }
            else
            {
                Log.debug("dialog.stask=null");
            }
            
        }

        private function onTransportTaskInfoRes(param1:TransportInfo) : void
        {
            if (view.isHide)
            {
                this.openView();
            }
            (view as IDialogModule).updateTransportTaskInfo(cache.dialog.dialogInfo, cache.dialog.transportInfo);
            
        }

        private function onTaskGetRes(param1:Object) : void
        {
            if (param1 == null)
            {
                
            }
            if (!param1.tasks || !param1.task)
            {
                
            }
            var _loc_2:* = param1.tasks;
            var _loc_3:* = param1.task;
            var _loc_4:* = param1.npcId;
            if (cache.dialog.npcInfo && _loc_4 == cache.dialog.npcInfo.tnpc.npcId)
            {
                switch(_loc_3.sTask.type)
                {
                    case ETaskType._ETaskTypeCatchBoss:
                    case ETaskType._ETaskTypeCollect:
                    case ETaskType._ETaskTypeGroupKillBoss:
                    case ETaskType._ETaskTypeKillBoss:
                    case ETaskType._ETaskTypeKillBossCount:
                    case ETaskType._ETaskTypeKillBossDrop:
                    {
                        if (_view.isHide)
                        {
                        }
                        break;
                    }
                    default:
                    {
                        if (!_view.isHide)
                        {
                        }
                        break;
                    }
                }
            }
            
        }

        private function onTaskEndRes(param1:Object) : void
        {
            if (param1 == null)
            {
                view.hide();
                
            }
            var _loc_2:* = param1.tasks;
            var _loc_3:* = param1.task;
            var _loc_4:* = param1.npcId;
            if (cache.dialog.npcInfo && _loc_4 == cache.dialog.npcInfo.tnpc.npcId)
            {
                if (cache.guide.guideItemAlert)
                {
                    view.hide();
                    
                }
                if (!view.isHide)
                {
                    if (!cache.guide.guideItemAlert)
                    {
                        (view as IDialogModule).endTaskRes();
                    }
                    view.hide();
                }
                cache.dialog.updateNpcTask(_loc_2);
                this.onTaskChange(cache.dialog.taskList);
            }
            
        }

        private function onTransportTalkRes(param1:TransportInfo) : void
        {
            if (!param1.transportAble)
            {
                if (view.isHide)
                {
                    this.openView();
                }
                (_view as IDialogModule).onTransportTalkRes(param1);
            }
            
        }

        private function onCanGetTaskAddRes(param1:Array) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:TaskInfo = null;
            if (_view && !_view.isHide)
            {
                _loc_3 = param1.length;
                while (_loc_2 < _loc_3)
                {
                    
                    _loc_4 = param1[_loc_2];
                    if (_loc_4.sTask.getNpc == cache.dialog.npcInfo.tnpc.npcId)
                    {
                        if ((_view as IDialogModule).viewStatus == 0 && cache.dialog.taskList.indexOf(_loc_4.sTask.code) == -1)
                        {
                            this.taskListReq();
                        }
                        break;
                    }
                    _loc_2++;
                }
            }
            
        }

        private function onTaskUpdataRes(param1:Array) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:TaskInfo = null;
            if (_view && !_view.isHide)
            {
                _loc_3 = param1.length;
                while (_loc_2 < _loc_3)
                {
                    
                    _loc_4 = param1[_loc_2];
                    if (_loc_4.status == ETaskStatus._ETaskStatusHadCompleted && _loc_4.sTask.endNpc == cache.dialog.npcInfo.tnpc.npcId)
                    {
                        if ((_view as IDialogModule).viewStatus == 0 && cache.dialog.taskList.indexOf(_loc_4.sTask.code) == -1)
                        {
                            this.taskListReq();
                        }
                        break;
                    }
                    _loc_2++;
                }
            }
            
        }

        private function onSceneUpdateHandler(event:DataEvent) : void
        {
            if (_view && !_view.isHide)
            {
                _view.hide();
            }
            
        }

        private function onGuildTaskInfoRes(param1:GuildTaskInfo) : void
        {
            if (view.isHide)
            {
                this.openView();
            }
            (_view as IDialogModule).onGuildTaskRes(param1, cache.dialog.dialogInfo);
            
        }

        private function onHuntTaskInfoRes(param1:HuntTaskInfo) : void
        {
            if (view.isHide)
            {
                this.openView();
            }
            (_view as IDialogModule).onHuntTaskRes(param1, cache.dialog.dialogInfo);
            
        }

        private function onGuildFlameInfoRes(param1:SGuildFlameInfo = null) : void
        {
            if (param1 != null)
            {
                (_view as IDialogModule).onGuildFlameRes(param1);
            }
            
        }

        private function openView() : void
        {
            if (this.checkDistance())
            {
                view.show();
            }
            
        }

        private function onWindowShow(event:WindowEvent) : void
        {
            NpcEffectRule.registDialogEffectWindow(view);
            
        }

        private function onWindowClose(event:WindowEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.NpcWindowClose));
            
        }

        private function onTaskChange(param1:Array) : void
        {
            var _loc_2:DialogInfo = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:Boolean = false;
            if (cache.guide.petGuide || cache.guide.guideSkillLearn || cache.guide.guideUseDrug || cache.guide.guideEquipRefresh && cache.guide.isGuideEquipRefresh(cache.dialog.npcInfo.tnpc.npcId) || cache.guide.guidePetRefresh && cache.guide.isGuidePetRefresh(cache.dialog.npcInfo.tnpc.npcId))
            {
                if (!view.isHide)
                {
                    view.hide();
                }
                
            }
            if (param1.length == 0)
            {
                if (!view.isHide)
                {
                    view.hide();
                }
            }
            else
            {
                _loc_3 = 0;
                _loc_4 = param1.length;
                _loc_5 = false;
                while (_loc_3 < _loc_4)
                {
                    
                    _loc_2 = param1[_loc_3];
                    if (_loc_2.status == ETaskStatus._ETaskStatusCanGet || _loc_2.status == ETaskStatus._ETaskStatusHadCompleted || _loc_2.status == ETaskStatus._ETaskStatusHadFail)
                    {
                        _loc_5 = true;
                        break;
                    }
                    _loc_3++;
                }
                if (_loc_5)
                {
                    this.onTaskListRes(param1);
                }
                else if (!view.isHide)
                {
                    view.hide();
                }
            }
            
        }

        private function checkOutDistance(event:Event) : void
        {
            var _loc_2:NPCPlayer = null;
            var _loc_3:int = 0;
            var _loc_4:IView = null;
            if (!this.checkDistance())
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AI_NpcOutDestance, cache.dialog.npcInfo));
                _loc_2 = ThingUtil.npcUtil.getNpc(cache.dialog.npcInfo.tnpc.npcId);
                if (_loc_2)
                {
                    _loc_2.selected = false;
                }
                while (NpcEffectRule.registedDialogWindow.length > 0)
                {
                    
                    _loc_4 = NpcEffectRule.registedDialogWindow[0];
                    if (_loc_4 && !_loc_4.isHide)
                    {
                        _loc_4.hide();
                    }
                    if (_loc_3 > 20)
                    {
                        break;
                    }
                    _loc_3++;
                }
                RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.checkOutDistance);
                Dispatcher.removeEventListener(EventName.Convey_Res, this.checkOutDistance);
                Dispatcher.removeEventListener(EventName.ChangeScene, this.checkOutDistance);
            }
            
        }

        private function checkDistance() : Boolean
        {
            var _loc_2:NPCPlayer = null;
            var _loc_1:* = Cache.instance.dialog.npcInfo;
            if (_loc_1)
            {
                _loc_2 = ThingUtil.npcUtil.getNpc(_loc_1.tnpc.npcId);
                if (_loc_2)
                {
                    if (ThingUtil.entityUtil.getDistanceByTile(_loc_2) <= GameConst.NpcTaskDistance)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        override protected function initView() : IView
        {
            var _loc_1:* = new DialogModule();
            _loc_1.addEventListener(EventName.NpcTaskInfoReq, this.onTaskInfoReq);
            _loc_1.addEventListener(EventName.NpcTaskDoReq, this.onTaskDoReq);
            _loc_1.addEventListener(EventName.NpcTaskGetReq, this.onTaskGetReq);
            _loc_1.addEventListener(EventName.NpcTaskEndReq, this.onTaskEndReq);
            _loc_1.addEventListener(EventName.NpcOptReq, this.onNpcOptReq);
            _loc_1.addEventListener(WindowEvent.CLOSE, this.onWindowClose);
            _loc_1.addEventListener(WindowEvent.SHOW, this.onWindowShow);
            return _loc_1;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.AI_NpcInDestance, this.onInNpcDestance);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.DialogTaskListRes, this.onTaskListRes);
            NetDispatcher.addCmdListener(ServerCommand.DialogTaskInfoRes, this.onTaskInfoRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.onTaskGetRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.onTaskEndRes);
            NetDispatcher.addCmdListener(ServerCommand.TransportTalkRes, this.onTransportTalkRes);
            NetDispatcher.addCmdListener(ServerCommand.DialogTransportTaskInfoRes, this.onTransportTaskInfoRes);
            NetDispatcher.addCmdListener(ServerCommand.CanGetListAdd, this.onCanGetTaskAddRes);
            NetDispatcher.addCmdListener(ServerCommand.CanGetListRefresh, this.onCanGetTaskAddRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.onTaskUpdataRes);
            NetDispatcher.addCmdListener(ServerCommand.GuildTaskInfoRes, this.onGuildTaskInfoRes);
            NetDispatcher.addCmdListener(ServerCommand.HuntTaskInfoRes, this.onHuntTaskInfoRes);
            NetDispatcher.addCmdListener(ServerCommand.GuildFlameInfoUpdate, this.onGuildFlameInfoRes);
            
        }

    }
}
